package youcaihuaoa.weetop.com.vm.main

import android.app.Application
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.CustomListBean
import com.robot.base.model.CustomListItemBean
import com.robot.base.model.UserInfoBean
import com.robot.base.util.ToastUtils
import com.robot.base.util.arouter.ARouterUtil
import com.tencent.connect.UserInfo
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import youcaihuaoa.weetop.com.R

class AddFriendViewModel(application: Application) : GLBaseViewModel(application) {

    val keyword = ObservableField<String>()

    val uiEvent = UIEvent()

    class UIEvent {

        val menuDataLiveEvent = SingleLiveEvent<List<UserInfoBean>>()

    }

    val clickJumpMyApplyListCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_MY_APPLY_LIST)
    })


    fun fetchFriendList() {
        val key = keyword.get() ?: ""
        val params = mapOf("keywords" to key)
        UserServiceFactory.fetchFriend(params)
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<UserInfoBean>>>(this) {
                override fun onNext(t: BaseResponse<List<UserInfoBean>>) {
                    uiEvent.menuDataLiveEvent.postValue(t.data)
                }
            })
    }

    fun addFriend(userid:String){
        val params = mapOf("user_id" to userid)
        UserServiceFactory.addFriend(params)
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<Any>>(this) {
                override fun onNext(t: BaseResponse<Any>) {
                    ToastUtils.showShort("添加成功!")
                }
            })
    }
}